import common from './common.js'
import { api } from './api.js'
const login = {
	uniLogin() {
		
		// #ifdef H5 
		this.h5Login()
		// #endif
		
		// #ifdef APP-PLUS
		this.appLogin()
		// #endif
		
		// #ifdef MP-WEIXIN
		this.mpWeiXinLogin()
		// #endif
	},
	
	h5Login() {
		common.getWechatCode() 
	},
	
	mpWeiXinLogin() {},
	
	appLogin() {
		uni.login({
				provider: 'weixin',
				success: function(loginRes) {
					// 获取用户信息
					uni.getUserInfo({
						provider: 'weixin',
						success: function(res) {
							res = res.userInfo
							let sourceUserId = common.getStorage('sourceUserId')
							let temp = { openId: res.openId, nickName: res.nickName, avatarUrl: res.avatarUrl, unionId: res.unionId, gender: res.gender, sourceUserId }
							api.loginFromAppAsync(temp, res => {
								let now = new Date()
								let nowTime = now.getTime()
								let expireTime = now.setTime(nowTime + 5 * 24 * 60 * 60 * 1000)
								res.expireTime = now.getTime()
								common.setUserInfo(res)
							})
						},
						fail(errGetUserInfo) {
							console.error(errGetUserInfo);
						}
					});
				},
				fail(errLogin) {
					console.error(errLogin);
				}
			});
	},
	
}

export { login } 